package com.cms.intelligent_control.instructions

import android.app.Activity
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cms.intelligent_control.R
import com.cms.intelligent_control.common.http.Api
import com.cms.intelligent_control.config.Config
import com.cms.intelligent_control.module.VideoModule
import com.cms.intelligent_control.utils.toList
import io.reactivex.android.schedulers.AndroidSchedulers
import org.json.JSONObject

class MonitorListCmd(var activity: Activity, private val videoModule: VideoModule) {
    private val mRecyclerView = activity.findViewById<RecyclerView>(R.id.rv_audio)

    private var mMonitorList: MutableList<JSONObject> = mutableListOf()
    private var mCmdKeyword = mutableListOf<String>()
    private var isShowMonitorList = false


    init {
        mRecyclerView.layoutManager = LinearLayoutManager(activity)
        mRecyclerView.addItemDecoration(
            DividerItemDecoration(
                activity,
                DividerItemDecoration.VERTICAL
            )
        )
    }

    public fun isMonitorListAction(data: JSONObject): Boolean {
        val type = data.optString("instruct_type")
        return arrayOf("监控源查询").contains(type)
    }

    public fun execute(data: JSONObject) {
        val type = data.optString("instruct_type")
        this.getMonitorList()
    }

    public fun executeLocalCmd(msg: String) {
        if (!isShowMonitorList) {
            return
        }
        if (videoModule.getPlayStatus() != VideoModule.Status.STATUS_READY) {
            return
        }
        val hitIndex = getHitAudioVideoIndex(msg)
        if (hitIndex != -1) {
            var obj = JSONObject()
            obj.put("instruct_type", "音视频播放")
            obj.put(
                "exe_operation",
                mMonitorList[hitIndex].get("content")
            )
            videoModule.execute(obj)
            mRecyclerView.adapter?.notifyDataSetChanged()
        }
    }

    private fun getMonitorList() {
        val d = Api.getApi().getMonitorSource()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.getInt("status") == 0) {
                    var list = it.optJSONArray("data").toList()
                    if (list.size > 5) {
                        list = list.subList(0, 5)
                    }
                    showMonitorList(list)
                }
            }, {
                Log.e("MonitorListCmd", "", it)
            })
    }


    public fun showMonitorList(list: MutableList<JSONObject>) {
        this.mMonitorList = list
        this.getCmdKeyword(list)
        val adapter =
            object : BaseQuickAdapter<JSONObject, BaseViewHolder>(R.layout.item_audio, list) {
                override fun convert(holder: BaseViewHolder, item: JSONObject) {
                    holder.setText(
                        R.id.tv_audio,
                        "${holder.layoutPosition + 1}、${item.optString("name")}"
                    )
                    holder.setImageResource(R.id.iv_icon, R.mipmap.audio_n)
                    holder.setVisible(R.id.iv_play, false)
                }
            }
        mRecyclerView.adapter = adapter
        mRecyclerView.visibility = View.VISIBLE
        isShowMonitorList = true
    }

    public fun hideMonitorList() {
        mMonitorList.clear()
        mCmdKeyword.clear()
        isShowMonitorList = false
        mRecyclerView.visibility = View.GONE
    }


    private fun getCmdKeyword(list: MutableList<JSONObject>) {
        mCmdKeyword.clear()
        for (i in 0 until list.size) {
            mCmdKeyword.add("${list[i].optString("name")}|${Constant.defaultKey[i]}")
        }
    }

    private fun getHitAudioVideoIndex(msg: String): Int {
        for (i in 0 until mCmdKeyword.size) {
            if (msg.contains(Regex(mCmdKeyword[i]))) {
                return i
            }
        }
        return -1
    }
}